﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SubRootItem : MonoBehaviour, INormalItem
{
    public Transform inspectorRoot;
    public Text itemName;
    public UGUIToggle toggle;

    private ActionSub subType;
    private Data.Action parent;
    private Data.ActionInterrupt intrData;
    private Data.AttackDef attData;
    private Data.Event eventData;

    private bool subItemSelect = false;
    private GameObject selectionObject;
    public void Reset()
    {
        gameObject.SetActive(false);
        parent = null;
        intrData = null;
        attData = null;
        eventData = null;
        toggle.onValueChangedEvent = null;
        toggle.isOn = false;
    }
    private void OnEnable()
    {
        toggle.onValueChangedEvent = OnChange;
    }
    private void OnDisable()
    {
        Reset();
    }
    private void OnChange(object obj , bool value)
    {
        subItemSelect = value;
        if (subItemSelect == true)
        {
            EventManager.TriggerEvent<ActionSub, object>(CommonEvent.SubPanel_SubToggleSelect, subType, GetSubData());
            ActionEditorInterface.SelectObj(selectionObject);
        }
    }
    public object GetSubData()
    {
        if (subType == ActionSub.Interrupt)
        {
            return intrData;
        }
        else if (subType == ActionSub.Event)
        {
            return eventData;
        }
        else if (subType == ActionSub.AttackDef)
        {
            return attData;
        }
        return null;
    }
    public void SetData(Data.ActionInterrupt intrData,Data.Action action)
    {
       
        this.parent = action;
        this.subType = ActionSub.Interrupt;
        this.intrData = intrData;
        itemName.text = intrData.ActionID +"_"+intrData.InterruptName;

        Destroy(selectionObject);
        selectionObject = new GameObject("ActionInterrupt_"+intrData.ActionID);
        var comp = selectionObject.AddComponent<InterruptInspctorData>();
        comp.SetData(intrData);
        selectionObject.transform.SetParent(inspectorRoot.transform);
    }

    public void SetData(Data.AttackDef attData, Data.Action action)
    {
        this.parent = action;
        this.subType = ActionSub.AttackDef;
        this.attData = attData;
        if (string.IsNullOrEmpty(attData.ID))
        {
            attData.ID = "未命名ID";
        }
        if (string.IsNullOrEmpty(attData.Name))
        {
            attData.Name = "未命名Name";
        }
        itemName.text = attData.ID + "_" + attData.Name;

        Destroy(selectionObject);
        selectionObject = new GameObject("AttackDefInspector_" + attData.Name);
        var comp = selectionObject.AddComponent<AttackDefInspectorData>();
        comp.SetData(attData);
        selectionObject.transform.SetParent(inspectorRoot.transform);
    }
    public void SetData(Data.Event eventData, Data.Action action)
    {
        this.parent = action;
        this.subType = ActionSub.Event;
        this.eventData = eventData;
        itemName.text = eventData.EventType.ToString()+"_"+eventData.Desc;

        Destroy(selectionObject);
        selectionObject = new GameObject("EventInspector_" + eventData.EventType.ToString());
        var comp = selectionObject.AddComponent<EventInspectorData>();
        comp.SetData(eventData);
        selectionObject.transform.SetParent(inspectorRoot.transform);
    }

    public void DelateData()
    {
        if (subType == ActionSub.Interrupt)
        {
            parent.ActionInterrupts.Remove(intrData);
        }
        else if (subType == ActionSub.AttackDef)
        {
            parent.AttackDefs.Remove(attData);
        }
        else if (subType == ActionSub.Event)
        {
            parent.Events.Remove(eventData);
        }
        EventManager.TriggerEvent<ActionSub>(CommonEvent.ActionSubItemDelate, subType);
    }

    public void OnRecycle()
    {
        
    }
}
